package com.dhnsoft.代码随想录.数组;

import java.lang.reflect.Array;
import java.util.Arrays;

/**
 * ClassName: 有序数组的平方
 * Package: com.dhnsoft.代码随想录
 * Description:
 *
 * @Author dhn
 * @Create 2024/2/29 9:35
 * @Version 1.0
 */
public class 有序数组的平方 {
    public static void main(String[] args) {
        int[] nums = new int[]{-4,-1,0,3,10};
        int[] ints = sortedSquares(nums);
        System.out.println(Arrays.toString(ints));
    }

    public static int[] sortedSquares(int[] nums) {
        int l = 0;
        int r = nums.length-1;
        int[] target = new int[nums.length];
        int k = nums.length-1;
        while (l<=r){
            if(Math.abs(nums[l])>Math.abs(nums[r])){
                target[k--] = (int) Math.pow(nums[l],2.0);
                l++;
            }
            else{
                target[k--] = (int)Math.pow(nums[r],2.0);
                r--;
            }

        }
        return target;
    }
}
